********************************************************************************
** 	TITLE:		nw2012_gles_contact_networks                                  ** 	
**  AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       ZA5333_v1-1-0                                                 **
**  DATE:	    October 2022 						                          **	
**	VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "nw2012_gles_contact_networks", replace

* Open the dataset

use "ZA5333_v1-1-0.dta", clear


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

foreach var of varlist endtime {
g `var'_date = date(`var', "DMYhms")
replace `var'_date = date(`var', "DMY") if `var'_date==. & `var'!=""
format `var'_date %td
g `var'_time = clock(`var', "DMYhms")
format `var'_time %tc_HH:MM:SS
}

generate surveydate = endtime_date

format %tdMon_DD,_CCYY surveydate

gen edate = 20120513

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time = (electiondate - surveydate) + 1 // Add 1 to correct for election date

* Gender

gen male = l1

recode male (2=0)

* Age

gen age = 2012 - l2

replace age = . if age < 17

* Education

gen education = l3

recode education (6=.)

* Interest for election

gen interest = l33

recode interest (99=.)

replace interest = 6 - interest

* Coalition preference

gen coalprefA = l108a
gen coalprefB = l108b
gen coalprefC = l108c
gen coalprefD = l108d
gen coalprefE = l108e
gen coalprefF = l108f
gen coalprefG = l108g
gen coalprefH = l108h
gen coalprefI = l108i
gen coalprefJ = l108j

recode coalprefA coalprefB coalprefC coalprefD coalprefE coalprefF coalprefG coalprefH coalprefI coalprefJ (99=.)

gen coalpref_complete = 1 if coalprefA!=. & coalprefB!=. & coalprefC!=. & coalprefD!=. & coalprefE!=. & coalprefF!=. & coalprefG!=. & coalprefH!=. & coalprefI!=. & coalprefJ!=.

gen coalpref = 3 if coalprefG >= coalprefA & coalprefG >= coalprefB & coalprefG >= coalprefC & coalprefG >= coalprefD & coalprefG >= coalprefE & coalprefG >= coalprefF & coalprefG >= coalprefH & coalprefG >= coalprefI & coalprefG >= coalprefJ & coalpref_complete == 1
replace coalpref = 2 if coalprefA == coalprefB & coalprefA == coalprefC & coalprefA == coalprefD & coalprefA == coalprefE & coalprefA == coalprefF & coalprefA == coalprefG & coalprefA == coalprefH & coalprefA == coalprefI & coalprefA == coalprefJ & coalpref_complete == 1
replace coalpref = 1 if coalpref!=2 & coalpref!=3 & coalpref_complete == 1

replace coalpref = . if coalpref == 3 & coalprefG == coalprefA & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefG == coalprefB & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefG == coalprefC & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefG == coalprefD & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefG == coalprefE & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefG == coalprefF & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefG == coalprefH & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefG == coalprefI & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefG == coalprefJ & coalpref_complete == 1

label define coalpref 1 "Loser coalition" 2 "No preference" 3 "Winner coalition"
label values coalpref coalpref

* Coalition preference strength

gen coalstrengthA = coalprefA if coalprefA >= coalprefB & coalprefA >= coalprefC & coalprefA >= coalprefD & coalprefA >= coalprefE & coalprefA >= coalprefF & coalprefA >= coalprefG & coalprefA >= coalprefH & coalprefA >= coalprefI & coalprefA >= coalprefJ & coalpref_complete == 1
gen coalstrengthB = coalprefB if coalprefB >= coalprefA & coalprefB >= coalprefC & coalprefB >= coalprefD & coalprefB >= coalprefE & coalprefB >= coalprefF & coalprefB >= coalprefG & coalprefB >= coalprefH & coalprefB >= coalprefI & coalprefB >= coalprefJ & coalpref_complete == 1
gen coalstrengthC = coalprefC if coalprefC >= coalprefA & coalprefC >= coalprefB & coalprefC >= coalprefD & coalprefC >= coalprefE & coalprefC >= coalprefF & coalprefC >= coalprefG & coalprefC >= coalprefH & coalprefC >= coalprefI & coalprefC >= coalprefJ & coalpref_complete == 1
gen coalstrengthD = coalprefD if coalprefD >= coalprefA & coalprefD >= coalprefB & coalprefD >= coalprefC & coalprefD >= coalprefE & coalprefD >= coalprefF & coalprefD >= coalprefG & coalprefD >= coalprefH & coalprefD >= coalprefI & coalprefD >= coalprefJ & coalpref_complete == 1
gen coalstrengthE = coalprefE if coalprefE >= coalprefA & coalprefE >= coalprefB & coalprefE >= coalprefC & coalprefE >= coalprefD & coalprefE >= coalprefF & coalprefE >= coalprefG & coalprefE >= coalprefH & coalprefE >= coalprefI & coalprefE >= coalprefJ & coalpref_complete == 1
gen coalstrengthF = coalprefF if coalprefF >= coalprefA & coalprefF >= coalprefB & coalprefF >= coalprefC & coalprefF >= coalprefD & coalprefF >= coalprefE & coalprefF >= coalprefG & coalprefF >= coalprefH & coalprefF >= coalprefI & coalprefF >= coalprefJ & coalpref_complete == 1
gen coalstrengthG = coalprefG if coalprefG >= coalprefA & coalprefG >= coalprefB & coalprefG >= coalprefC & coalprefG >= coalprefD & coalprefG >= coalprefE & coalprefG >= coalprefF & coalprefG >= coalprefH & coalprefG >= coalprefI & coalprefG >= coalprefJ & coalpref_complete == 1
gen coalstrengthH = coalprefH if coalprefH >= coalprefA & coalprefH >= coalprefB & coalprefH >= coalprefC & coalprefH >= coalprefD & coalprefH >= coalprefE & coalprefH >= coalprefF & coalprefH >= coalprefG & coalprefH >= coalprefI & coalprefH >= coalprefJ & coalpref_complete == 1
gen coalstrengthI = coalprefI if coalprefI >= coalprefA & coalprefI >= coalprefB & coalprefI >= coalprefC & coalprefI >= coalprefD & coalprefI >= coalprefE & coalprefI >= coalprefF & coalprefI >= coalprefG & coalprefI >= coalprefH & coalprefI >= coalprefJ & coalpref_complete == 1
gen coalstrengthJ = coalprefI if coalprefJ >= coalprefA & coalprefJ >= coalprefB & coalprefJ >= coalprefC & coalprefJ >= coalprefD & coalprefJ >= coalprefE & coalprefJ >= coalprefF & coalprefJ >= coalprefG & coalprefJ >= coalprefH & coalprefJ >= coalprefI & coalpref_complete == 1

gen coalstrength = max(coalstrengthA,coalstrengthB,coalstrengthC,coalstrengthD,coalstrengthE,coalstrengthF,coalstrengthG,coalstrengthH,coalstrengthI,coalstrengthJ)

replace coalstrength = 300 if coalstrength == 8 | coalstrength == 9 | coalstrength == 10 | coalstrength == 11
replace coalstrength = 200 if coalstrength == 5 | coalstrength == 6 | coalstrength == 7
replace coalstrength = 100 if coalstrength == 1 | coalstrength == 2 | coalstrength == 3 | coalstrength == 4

replace coalstrength = 3 if coalstrength == 300
replace coalstrength = 2 if coalstrength == 200
replace coalstrength = 1 if coalstrength == 100

label define coalstrength 1 "Weak preference" 2 "Moderate preference" 3 "Strong preference"
label values coalstrength coalstrength

* Coalition preference scale

gen coalscale = .

replace coalscale = 1 if coalpref == 1 & coalstrength == 3
replace coalscale = 2 if coalpref == 1 & coalstrength == 2
replace coalscale = 3 if coalpref == 1 & coalstrength == 1
replace coalscale = 4 if coalpref == 2
replace coalscale = 5 if coalpref == 3 & coalstrength == 1
replace coalscale = 6 if coalpref == 3 & coalstrength == 2
replace coalscale = 7 if coalpref == 3 & coalstrength == 3

label define coalscale 1 "Strong loser preference" 2 "Moderate loser preference" 3 "Weak loser preference" 4 "No preference" 5 "Weak winner preference" 6 "Moderate winner preference" 7 "Strong winner preference" 
label values coalscale coalscale

* Political discussion

gen discussion = l148

recode discussion (99=.)
recode discussion (100=.)

gen discussion_3pts = 0 if discussion < 2 & discussion!=.
replace discussion_3pts = 1 if discussion >= 2 & discussion < 4 & discussion!=.
replace discussion_3pts = 2 if discussion >= 4 & discussion!=.

* Network expertise

gen expertise = l151

recode expertise (99=.)
recode expertise (100=.)

replace expertise =  4 - expertise

replace expertise = 0 if discussion == 0

* Political disagreement

gen disagreement = l152

recode disagreement (99=.)
recode disagreement (100=.)

replace disagreement = 4 - disagreement

gen disagreement_3pts = disagreement
replace disagreement_3pts = 0 if disagreement == 1
replace disagreement_3pts = 1 if disagreement == 2
replace disagreement_3pts = 2 if disagreement == 3


*********************************
** NATIONAL-LEVEL EXPECTATIONS ** 
*********************************

gen winCoalitionA = l107a
gen winCoalitionB = l107b
gen winCoalitionC = l107c
gen winCoalitionD = l107d
gen winCoalitionE = l107e
gen winCoalitionF = l107f
gen winCoalitionG = l107g

recode winCoalitionA winCoalitionB winCoalitionC winCoalitionD winCoalitionE winCoalitionF winCoalitionG (99=.)

gen correct_whole_d = 1 if winCoalitionD == 1

replace correct_whole_d = 0 if winCoalitionA == 1
replace correct_whole_d = 0 if winCoalitionB == 1
replace correct_whole_d = 0 if winCoalitionC == 1
replace correct_whole_d = 0 if winCoalitionD == 0
replace correct_whole_d = 0 if winCoalitionE == 1
replace correct_whole_d = 0 if winCoalitionF == 1
replace correct_whole_d = . if winCoalitionG == 1


**********
** SAVE **
**********

save "nw2012_gles.dta", replace


*******************
** MAIN ANALYSES **
*******************

* Regression analysis and percentage of correct and incorrect forecasts

logistic correct_whole_d discussion disagreement expertise coalscale interest age i.male education time  
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1


************************
** SUMMARY STATISTICS **
************************

label variable correct_whole_d "Coalition formed (DV)"
label variable discussion "Discussion"
label variable disagreement "Disagreement"
label variable expertise "Network expertise"
label variable coalscale "Coalition preference"
label variable interest "Interest"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"

estpost tabstat correct_whole_d discussion disagreement expertise coalscale interest age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "nw2012_gles_summary_regional.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2012 North Rhine-Westphalia regional election (GLES), regional level") replace

eststo clear

log close